package n148;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 苏文广
 * @since 2023/4/13
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(new Solution().mid(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))))).val);
    }

    public ListNode mid(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode sortList(ListNode head) {
        if(head == null){
            return null;
        }
        List<Integer> values = new ArrayList<>();
        ListNode temp = head;
        while (temp != null){
            values.add(temp.val);
            temp = temp.next;
        }

        values.sort(Integer::compareTo);

        ListNode resultHead = new ListNode();
        temp = resultHead;
        for (Integer value : values) {
            temp.next = new ListNode(value);
            temp = temp.next;
        }

        return  resultHead.next;


    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
